package it.afs.worklog.application.form.admin;

import it.afs.utils.PropertyManager;
import it.afs.wicket.PatternDateConverter;
import it.afs.worklog.Cliente;
import it.afs.worklog.Commessa;
import it.afs.worklog.application.form.ClienteDropDownChoice;

import java.util.Date;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.datetime.markup.html.basic.DateLabel;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

/**
 * @author Fanta
 */
public class CommessePage extends AdminPage
{
	private Model<Cliente> clienteModel;
	
	public CommessePage()
	{
		super();
		
		clienteModel = new Model<Cliente>();
		
		addComponents();
	}
	
	public CommessePage(PageParameters parameters)
	{
		super(parameters);
		
		clienteModel = new Model<Cliente>(getFactory().getDao().getCliente(parameters.getString("cliente")));
		
		addComponents();
	}

	@Override
	protected void addComponents()
	{
		super.addComponents();
		
		add(getHomePageLink());
		
		ClienteDropDownChoice cliente = new ClienteDropDownChoice("cliente", clienteModel, getFactory().getDao().getClienti())
		{
			private static final long serialVersionUID = 8822940738106720553L;

			@Override
			protected boolean wantOnSelectionChangedNotifications() {
				return true;
			}
		};
		cliente.setShowDisabled(true);
		add(cliente);
		
		LoadableDetachableModel<List<Commessa>> commesseModel = new LoadableDetachableModel<List<Commessa>>()
		{
			private static final long serialVersionUID = 302388989018898881L;

			@Override
			protected List<Commessa> load()
			{
				return getFactory().getDao().getCommesse(clienteModel.getObject());
			}
		};
		
		WebMarkupContainer listPanel = new WebMarkupContainer("list-panel")
		{
			private static final long serialVersionUID = 7510740241989262525L;

			@Override
			protected void onConfigure()
			{
				super.onConfigure();
				setVisible(clienteModel.getObject() != null);
			}
		};
		add(listPanel);
		
		ListView<Commessa> commesse = new ListView<Commessa>("commesse", commesseModel)
		{
			private static final long serialVersionUID = 5345789723011504230L;

			@Override
			protected void populateItem(ListItem<Commessa> item)
			{
				item.add(new Label("codice", new PropertyModel<String>(item.getModel(), "codice")));
				item.add(new Label("titolo", new PropertyModel<String>(item.getModel(), "titolo")));
				item.add(new Label("data", new PropertyModel<String>(item.getModel(), "data")));
				item.add(new Label("descrizione", new PropertyModel<String>(item.getModel(), "descrizione")));
				item.add(new Label("estremi", new PropertyModel<String>(item.getModel(), "estremi")));
				item.add(new Label("importo", new PropertyModel<String>(item.getModel(), "importo")));
				item.add(new DateLabel("il", new PropertyModel<Date>(item.getModel(), "inizioLavori"), new PatternDateConverter(PropertyManager.getValue("date.shortformat"))));
				item.add(new DateLabel("fl", new PropertyModel<Date>(item.getModel(), "fineLavori"), new PatternDateConverter(PropertyManager.getValue("date.shortformat"))));
				
				item.add(new WebMarkupContainer("attivo", new PropertyModel<Boolean>(item.getModel(), "attivo"))
				{
					private static final long serialVersionUID = -2807372166352478551L;

					@Override
					protected void onConfigure()
					{
						super.onConfigure();
						setVisible((Boolean)this.getDefaultModelObject());
					}
				});
				item.add(new Link<String>("edit", new PropertyModel<String>(item.getModel(), "id"))
				{
					private static final long serialVersionUID = 6810306346153024920L;

					@Override
					public void onClick()
					{
						setResponsePage(CommessaFormPage.class, new PageParameters("id=" + getModelObject()));
					}
				});
				item.add(new Link<String>("attivita", new PropertyModel<String>(item.getModel(), "id"))
				{
					private static final long serialVersionUID = 3437927571276277262L;

					@Override
					public void onClick()
					{
						setResponsePage(AttivitaPage.class, new PageParameters("commessa=" + getModelObject()));
					}
				});
			}
		};
		listPanel.add(commesse);
		
		listPanel.add(new Link<Cliente>("add", clienteModel)
		{
			private static final long serialVersionUID = -6624313122924797188L;

			@Override
			public void onClick()
			{
				setResponsePage(CommessaFormPage.class, new PageParameters("cliente=" + getModelObject().getId()));
			}
		});
	}
}